﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.Frame.Architecture;
using Resto.CostControl.Core.Domain.Query.SiteModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Facade.SiteModule.DataTransferObject;
using Resto.CostControl.Core.Enums;
using Resto.CostControl.Core.Domain.Model;
using Enterprise.Framework.Core.Exceptions;

namespace Resto.CostControl.Core.Facade.SiteModule.FacadeObject
{
    /// <summary>
    /// 站点成本FO
    /// </summary>
    public class SiteCostFO : IApplicationFacadeObject
    {
        #region 私有属性

        private SiteCostDQO _SiteCostDQO;
        private SiteCostDQO SiteCostDQO
        {
            get
            {
                if (_SiteCostDQO == null)
                    _SiteCostDQO = ObjectCreator.Create<SiteCostDQO>();

                return _SiteCostDQO;
            }
        }

        #endregion

        #region 公共方法

        public virtual void AddSiteCost(SiteCostDTO contractDTO, DataState state)
        {
            SiteCost cost = contractDTO.ToSiteCost();
            cost.State = state.ToString();

            cost.Add();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            contractDTO.ID = cost.ID;
        }

        public virtual void UpdateSiteCost(SiteCostDTO costDTO, DataState state)
        {
            SiteCost cost = SiteCostDQO.GetSiteCost(costDTO.ID);

            if (cost.State == DataState.Publish.ToString())
                throw new BusinessException("对不起，数据已经提交不能修改");

            cost.State = state.ToString();

            costDTO.CopyToSiteCost(cost);

            cost.Update();


            RestoCostDomainSession.CurrentSession.SaveChanges();

        }

        public virtual void DeleteSiteCost(string id)
        {
            SiteCost supplierContract = SiteCostDQO.GetSiteCost(id);

            supplierContract.Delete();

            RestoCostDomainSession.CurrentSession.SaveChanges();

        }

        public virtual SiteCostDTO GetSiteCostDTO(string id)
        {
            SiteCost supplierContract = SiteCostDQO.GetSiteCost(id);

            return new SiteCostDTO(supplierContract);
        }

        #endregion
    }
}
